package org.jetlinks.pro.calculator;

import io.swagger.v3.oas.annotations.media.Schema;
import org.jetlinks.core.metadata.DataType;
import org.jetlinks.core.metadata.types.DataTypes;
import org.jetlinks.core.metadata.types.DoubleType;
import org.jetlinks.core.metadata.types.NumberType;
import org.jetlinks.reactor.ql.utils.CastUtils;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author bestfeng
 * @since 1.9
 */
public class NumberValueCalculateSupport implements ValueCalculateSupport {

    @Schema(description = "初始值，数据类型为数字时使用")
    private final double initialValue;

    @Schema(description = "倍数, 数据类型为数字时使用")
    private final double multiple;

    private final DoubleType numberType = DoubleType.GLOBAL;

    public NumberValueCalculateSupport(Map<String, Object> configuration) {
        Object initialValue = configuration.getOrDefault("initialValue", 0D);
        Object multiple = configuration.getOrDefault("multiple", 1D);
        this.initialValue = CastUtils.castNumber(initialValue).doubleValue();
        this.multiple = CastUtils.castNumber(multiple).doubleValue();

    }

    @Override
    public Object compute(Object value) {
        //never happen
//        if (StringUtils.isEmpty(initialValue) && StringUtils.isEmpty(multiple)) {
//            return value;
//        }
        return numberType.convert(value) * multiple + initialValue;
    }
}
